import CryptoJS,{enc,AES,mode,pad, SHA256} from 'crypto-js'

export interface CrypotoType {
    encrypt: any
    decrypt: any
}

export default class Crypoto implements CrypotoType {
   private key = CryptoJS.enc.Utf8.parse('123456789asdfghj') // 十六位十六进制数作为密钥
    iv = CryptoJS.enc.Utf8.parse('asdfghj123456789') // 十六位十六进制数作为密钥偏移量

    /* 加密 */
    encrypt (word: any) {
        const srcs = CryptoJS.enc.Utf8.parse(word)
        const encrypted = CryptoJS.AES.encrypt(srcs, this.key, { iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
        return encrypted.ciphertext.toString().toUpperCase()
    }

    /* 解密 */
    decrypt (word: any) {
        const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
        const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
        const decrypt = CryptoJS.AES.decrypt(srcs, this.key, { iv: this.iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
        const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
        return decryptedStr.toString()
    }
    /**
     * AES加密
     */
    encryptByEnAES(data: string): string {
        // @ts-ignore
        let Key = enc.Utf8.parse(this.key);
        let tmpAES = AES.encrypt(enc.Utf8.parse(data), Key, {
            mode: mode.ECB,
            padding: pad.Pkcs7
        });
        return tmpAES.toString();
    }
    /**
     * AES解密
     */
    encryptByDeAES(data: string): string {
        // @ts-ignore
        let Key = enc.Utf8.parse(this.key);
        let tmpDeAES = AES.decrypt(data, Key, {
            mode: mode.ECB,
            padding: pad.Pkcs7
        });
        return tmpDeAES.toString(enc.Utf8);
    }

    /**
     *  SHA256 加密
     * @param data
     */
}
